package com.qf.controller;

import com.qf.pojo.req.XcUserReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Api(value = "用户登录注册接口",tags = "用户登录注册接口")
public class UserController {

    @Autowired
    UserService userService;

    //注册模块--1.前端验证格式  2.后台对比数据
    //1.当邮箱填写格式正确后触发事件将邮箱数据发送到后台判断该邮箱是否已经注册

    //验证邮箱---前台发起请求携带参数email来进行对比---只验证邮箱是否存在所以邮箱是以字符串类型用?携参发送过来
    @ApiOperation("验证邮箱---前台发起请求携带参数email来进行对比---只验证邮箱是否存在所以邮箱是以字符串类型用?携参发送过来")
    @RequestMapping(value = "/validataEmail",method = RequestMethod.GET)
    public BaseResp validataEmail(@RequestParam("email")String email){

        return userService.validataEmail(email);
    }

    //验证用户名是否唯一 ----与邮箱验证一样  密码验证前端校验
    @ApiOperation("验证用户名是否唯一")
    @RequestMapping(value = "/validataUserName",method = RequestMethod.GET)
    public BaseResp validataUserName(@RequestParam("username")String username){

        return userService.validataUserName(username);
    }

    //前台请求发送邮件验证码 请求数据是前台输入的邮箱
    @ApiOperation("前台请求发送邮件验证码 请求数据是前台输入的邮箱")
    @RequestMapping(value = "/getCode",method = RequestMethod.GET)
    public BaseResp getCode(@RequestParam("email")String email){

        return userService.getCode(email);
    }


    //用户注册
    @ApiOperation("用户注册")
    @RequestMapping(value = "/registry",method = RequestMethod.POST)
    public BaseResp registry(@RequestBody XcUserReq xcUserReq){
        System.out.println(xcUserReq);
        return userService.registry(xcUserReq);
    }


    //登录模块

    //用户登录
    @ApiOperation("用户登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public BaseResp login(@RequestBody XcUserReq xcUserReq){

        return userService.login(xcUserReq);
    }


    //忘记密码-重置密码（通过邮箱修改）
    @ApiOperation("忘记密码-重置密码（通过邮箱修改）")
    @RequestMapping(value = "/updatepasswordbyemail",method = RequestMethod.POST)
    public BaseResp updatepasswordbyemail(@RequestBody XcUserReq xcUserReq){

       return userService.updatepasswordbyemail(xcUserReq);
    }



    //拿到token然后解析返回前台 只要发生页面请求就会调用此方法
    @ApiOperation("拿到token然后解析返回前台只要发生页面请求就会调用此方法")
    @RequestMapping(value = "/findUserByToken",method = RequestMethod.POST)
    public BaseResp findUserByToken(HttpServletRequest request){

        //从登录成功的返回信息中拿到token值进行解析 返回对象
        //为保证安全性，将JWT的信息存放在HTTP的请求头中
        return userService.findUserByToken(request.getHeader("token").toString());
    }


    //退出
    @ApiOperation("退出.删除redis存储的信息")
    @RequestMapping(value = "/exit",method = RequestMethod.GET)
    public BaseResp exit(HttpServletRequest request){
       return userService.exit(request);
    }
}
